package com.person.yhzs.user.controller;

import com.person.yhzs.annotation.Authorized;
import com.person.yhzs.common.ReturnData;
import com.person.yhzs.common.page.PageResponse;
import com.person.yhzs.user.entity.User;
import com.person.yhzs.user.entity.dto.LoginDto;
import com.person.yhzs.user.entity.dto.UserDto;
import com.person.yhzs.user.entity.vo.UserVo;
import com.person.yhzs.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author luolin
 * @version 1.0.0
 * @ClassName UserController.java
 * @Description TODO
 * @createTime 2022年05月30日 14:28:00
 */
@Api(tags = "用户")
@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * @Description //快捷登录
     * @Author luolin
     * @Date 2022/5/30 14:34
     **/
    @ApiOperation(value = "快捷登录",notes = "快捷登录接口")
    @GetMapping("/quickLogin/{phone}")
    public ReturnData quickLogin(@PathVariable String phone){
        Map<String,Object> login = userService.quickLogin(phone);
        return ReturnData.ok(login);
    }

    /**
     * @Description //微信用户手机号
     * @Author luolin
     * @Date 2022/5/30 14:34
     **/
    @ApiOperation(value = "微信用户手机号",notes = "微信用户手机号接口")
    @GetMapping("/wx/wxPhone/{code}")
    public ReturnData wxPhone(@PathVariable String code){
        Map<String,Object> login = userService.wxPhone(code);
        return ReturnData.ok(login);
    }

    /**
     * @Description //用户登录
     * @Author luolin
     * @Date 2022/5/30 14:34
     **/
    @ApiOperation(value = "微信用户登录",notes = "微信用户登录接口")
    @GetMapping("/wx/session/{code}")
    public ReturnData wxLogin(@PathVariable String code){
        Map<String,Object> login = userService.wxLogin(code);
        return ReturnData.ok(login);
    }

    /**
     * @Description //用户登录
     * @Author luolin
     * @Date 2022/5/30 14:34
     **/
    @ApiOperation(value = "用户登录",notes = "用户登录接口")
    @PostMapping("session")
    public ReturnData login(@RequestBody LoginDto loginDto){
        Map<String,Object> login = userService.login(loginDto);
        return ReturnData.ok(login);
    }

    /**
     * @Description //管理员登录
     * @Author luolin
     * @Date 2022/5/30 14:34
     **/
    @ApiOperation(value = "管理员登录",notes = "管理员登录接口")
    @PostMapping("admin/session")
    public ReturnData adminLogin(@RequestBody LoginDto loginDto){
        Map<String,Object> login = userService.login(loginDto);
        return ReturnData.ok(login);
    }

    /**
     * 用户注销
     *
     * @return
     */
    @Authorized
    @ApiOperation(value = "用户注销", notes = "用户注销接口")
    @DeleteMapping("session")
    public ReturnData logout(@ApiIgnore @RequestAttribute Integer userId) {
        userService.logout(userId);
        return ReturnData.ok("退出登录成功");
    }

    /**
     * @Description 新增用户
     * @Author luolin
     * @Date 2022/5/31 10:26
     **/
    @ApiOperation(value = "新增用户",notes = "新增用户接口")
    @PutMapping("insertUser")
    public ReturnData insertUser(@RequestBody UserDto userDto){
        userService.insertUser(userDto);
        return ReturnData.ok("操作成功");
    }

    /**
     * @Description 修改用户
     * @Author luolin
     * @Date 2022/5/31 10:26
     **/
    @Authorized
    @ApiOperation(value = "修改用户",notes = "修改用户接口")
    @PutMapping("updateUser")
    public ReturnData updateUser(@RequestBody UserDto userDto,@ApiIgnore @RequestAttribute Long userId){
        userDto.setId(userId.intValue());
        System.out.println(userId);
        userService.updateUser(userDto);
        return ReturnData.ok("操作成功");
    }

    /**
     * @Description 修改用户
     * @Author luolin
     * @Date 2022/5/31 10:26
     **/
    @Authorized
    @ApiOperation(value = "通过ID修改用户",notes = "通过ID修改用户接口")
    @PutMapping("updateUserById")
    public ReturnData updateUserById(@RequestBody UserDto userDto){
        userService.updateUser(userDto);
        return ReturnData.ok("操作成功");
    }

    /**
     * @Description 账号检测
     * @Author luolin
     * @Date 2022/5/31 10:26
     **/
    @ApiOperation(value = "账号检测",notes = "账号检测接口")
    @GetMapping("checkAccount")
    public ReturnData checkAccount(String account){
        userService.checkAccount(account);
        return ReturnData.ok("未注册").setCode(200);
    }

    @ApiOperation(value = "分页查询人员列表", notes = "人员列表接口")
    public ReturnData queryUsers(@RequestBody UserDto userDto){
        PageResponse<UserVo> userVoPageResponse = null;
//                userService.queryUsers(userDto);
        return ReturnData.ok(userVoPageResponse);
    }

    @ApiOperation(value = "手机号获取人员信息",notes = "手机号获取人员信息接口")
    @GetMapping("queryUserByPhone")
    public ReturnData queryUserByPhone(String phone){
        User user = userService.queryUserByPhone(phone);
        return ReturnData.ok(user);
    }

    @ApiOperation(value = "获取人员信息",notes = "获取人员信息接口")
    @GetMapping("queryUserById/{userId}")
    public ReturnData queryUserById(@PathVariable("userId") Long userId){
        User user = userService.queryUserById(userId);
        return ReturnData.ok(user);
    }
}
